{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "protective-stroke",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os \n",
    "from gnutools.fs import listfiles, name, parent\n",
    "from asr_deepspeech.audio import duration, fq\n",
    "from tqdm import tqdm\n",
    "from pprint import pprint\n",
    "import pandas as pd\n",
    "from concurrent.futures import ProcessPoolExecutor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "inappropriate-ivory",
   "metadata": {},
   "outputs": [],
   "source": [
    "root = \"/srv/sync/cdata/ASR/en/CLEAN/raw/GOOGLE\"\n",
    "files = [f for f in listfiles(root, [\".wav\"]) if f.endswith(\".wav\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "broke-conditioning",
   "metadata": {},
   "outputs": [],
   "source": [
    "class GoogleDataset():\n",
    "    def __init__(self, files, fq=16000):\n",
    "        self._files = files\n",
    "        self._fq = fq\n",
    "        self._entries = self.entries()\n",
    "    \n",
    "    def entries(self):\n",
    "        d = dict([self.entry(f) for f in tqdm(self._files, total=len(self._files))])\n",
    "        return pd.DataFrame.from_dict(d).transpose()\n",
    "            \n",
    "    def entry(self, file):\n",
    "        _name_file = name(file)\n",
    "        _duration = duration(file)\n",
    "        _fq = fq(file)\n",
    "        _text = name(parent(file))\n",
    "        assert _fq == self._fq\n",
    "        d = {\n",
    "            \"audio_filepath\": file,\n",
    "            \"duration\": _duration,\n",
    "            \"fq\": _fq,\n",
    "            \"text\": _text\n",
    "        }\n",
    "        return (_name_file,  d)\n",
    "    \n",
    "    def filter_duration(self, start=1, stop=5, to_dict=False):\n",
    "        df =  self._entries[(self._entries[\"duration\"]>=start) & (self._entries[\"duration\"]<=stop)]\n",
    "        return df.transpose().to_dict() if to_dict else df\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "running-conviction",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 7478/7478 [00:00<00:00, 12971.69it/s]\n"
     ]
    }
   ],
   "source": [
    "dataset = GoogleDataset(files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "forbidden-meeting",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "json.dump(dataset.filter_duration(1, 2, to_dict=True), open(\"train.json\", \"w\"), indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "elegant-mississippi",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:root] *",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
